import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  final ThemeData iOSTD = ThemeData(
      primaryColor: Colors.grey,
      iconTheme: IconThemeData(color: Colors.red),
      textTheme:
      TextTheme(bodyText2: TextStyle(color: Colors.deepOrange))
  );
  final ThemeData AndroidTD = ThemeData(
      primaryColor: Colors.white,
      iconTheme: IconThemeData(color: Colors.blue),
      textTheme:
      TextTheme(bodyText2: TextStyle(color: Colors.pink))
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
            Icon(Icons.favorite),
            // 新建主题
            Theme(

              data: ThemeData(
                  primaryColor: Colors.grey,
                  iconTheme: IconThemeData(color: Colors.red),
                  textTheme:
                      TextTheme(bodyText2: TextStyle(color: Colors.deepOrange))),
              child: LocalTheme(),
            ),
            // 继承主题
            Theme(
                data: Theme.of(context)
                    .copyWith(iconTheme: IconThemeData(color: Colors.green)),
                child: Icon(Icons.feedback)),
            //不同平台不同主题
            Theme(
              data: defaultTargetPlatform == TargetPlatform.iOS ? iOSTD: AndroidTD,
              child: LocalTheme(),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class LocalTheme extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Row(children: <Widget>[
          Icon(Icons.favorite),
          //需要在Widget的父层Widget去设置局部Theme，才会生效；
          //如果我们直接在局部Theme的child 属性指定的Widget中去使用的话，会拿到系统的全局Theme.
          //Icon可以 Text不行
          Text(
            '德玛西亚',
            style: Theme.of(context).textTheme.bodyText2,
          ),
        ]),
        Container(
          color: Theme.of(context).primaryColor, //容器背景色复用应用主题色
          child: Text(
            'Text with a background color',
            style: Theme.of(context).textTheme.bodyText2, //Text组件文本样式复用应用文本样式
          )),
      ],
    );
  }
}
